package com.cip.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;


import com.cip.webapp.action.BaseAction;
import com.cip.admin.model.Friends;
import com.cip.admin.model.User;
import com.cip.admin.service.FriendsService;
import com.cip.admin.service.UserManager;
import com.cip.common.util.CommonUtil;
import com.cip.common.util.SecurityUtil;

public class FriendsAction extends BaseAction{
	
	private FriendsService friendsService;
	private UserManager userManager;
	List<Map<String, String>> listofSuggests;
	
	public String saveOrUpdateFriends() throws Exception{
		Friends friends = new Friends();
		User user = SecurityUtil.getThreadLocalUser().get();
		friends.setRequestFriend(user);
		System.out.println("friend name========>"+getRequest().getParameter("userName"));
		User connectFriend = userManager.getUserByUsername(getRequest().getParameter("userName"));
		friends.setResponseFriend(connectFriend);
		friends.setTenantId(user.getTenantId());
		friends.setStatus("inActive");
		friendsService.saveOrUpdateFriends(friends);
		System.out.println("Created friend=========");
		return SUCCESS;
		
	}
	
	public String confirmFriends() throws Exception{
		User user = SecurityUtil.getThreadLocalUser().get();
		Friends requestedFriend = friendsService.getFriendByRequestAndResponse(Integer.valueOf(getRequest().getParameter("userId")), user.getId());
		requestedFriend.setStatus("Active");
		friendsService.saveOrUpdateFriends(requestedFriend);
		Friends friends = new Friends();		
		friends.setRequestFriend(user);
		friends.setResponseFriend(requestedFriend.getRequestFriend());
		friends.setTenantId(user.getTenantId());
		friends.setStatus("Active");
		friendsService.saveOrUpdateFriends(friends);
		return SUCCESS;
		
	}
	
	public String showAllFriends() throws Exception{
		User user = SecurityUtil.getThreadLocalUser().get();
		friendsService.getAllFriendsById(user.getId());
		return SUCCESS;
	}
	
	public String showAllUnConfirmedFriends() throws Exception{
		User user = SecurityUtil.getThreadLocalUser().get();
		friendsService.getAllUnConfirmedFriendsById(user.getId());
		return SUCCESS;
	}
	
	public void searchFriend() throws Exception{
		System.out.println("comes in search===="+getRequest().getParameter("searchName"));
		List<User> userList= friendsService.getFriendBySearchName(getRequest().getParameter("searchName"));
		System.out.println("user list sear===="+userList.size());
		listofSuggests = friendsService.convertFriendsAsMap(userList);
		JSONObject jsonobject = new JSONObject();
		System.out.println("searchFriend List size===="+listofSuggests.size());
		jsonobject.put("userName", listofSuggests);
		CommonUtil.generateResponseThroughJSON(getResponse(), jsonobject);
		//return SUCCESS;
	}

	public FriendsService getFriendsService() {
		return friendsService;
	}

	public void setFriendsService(FriendsService friendsService) {
		this.friendsService = friendsService;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	public List<Map<String, String>> getListofSuggests() {
		return listofSuggests;
	}

	public void setListofSuggests(List<Map<String, String>> listofSuggests) {
		this.listofSuggests = listofSuggests;
	}

}
